'''
Created on 2020年12月15日14:11:53

@author: Administrator
'''
import os

from flask import json

from common.public import *
from config import *
from view.baseview import BaseView


class HelpJsonView(BaseView):
    def __init__(self):
        pass

    def read(self):
        # HELP_PATH
        if not os.path.exists(HELP_PATH):
            # 指定的文件或目录不存在:
            os.makedirs(HELP_PATH) 
        for x in os.walk(HELP_PATH):
            dict = [x[0] + "\\" + target for target in x[2] if 'json' in target]
        data = []
        for row in dict:
            print(row)
            filename = row
            f_obj = open(filename)
            print(f_obj)
            j = json.load(f_obj)
            data.append(j)
            f_obj.close()
        # return data
        return super().resultSet(200, "", data)

    def readhelptree(self):
        if not os.path.exists(HELP_TREE_PATH):
            # 指定的文件或目录不存在:
            os.makedirs(HELP_TREE_PATH) 
        for x in os.walk(HELP_TREE_PATH):
            dict = [x[0] + "\\" + target for target in x[2] if 'json' in target]
        data = []
        f_obj = None
        try:
            for filename in dict:
                f_obj = open(filename)
                j = json.load(f_obj)
                data.append(j)
                f_obj.close()
        except IOError:
            f_obj.close()
        else:
            pass
#         for row in dict:
#             filename = row
#             f_obj = open(filename)
#             j = json.load(f_obj)
#             data.append(j)
#             f_obj.close()
        # return data
        return super().resultSet(200, "", data)
